﻿using _IMES.DBUtils;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using houseAgency.Models;
using houseAgency.View;
using MySql.Data.MySqlClient;
using Org.BouncyCastle.Asn1.X509;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Input;

namespace houseAgency.ViewModel
{
    public class LoginWindowViewModel : ObservableObject
    {
		private LoadingWindow loadingWindow = new LoadingWindow();

		private string userName = "admin";

		public string UserName
		{
			get { return userName; }
			set
			{
				userName = value;
				OnPropertyChanged();
			}
		}

		private string password = "admin";

		public string Password
		{
			get { return password; }
			set
			{
                password = value;
				OnPropertyChanged();
			}
		}

        public ICommand LoginCommand
        {
            get
            {
                return new RelayCommand<LoginWindow>(o =>
                {
					checkInfo(o, UserName, Password);
                });
            }
        }

		private void checkInfo(LoginWindow o, string userName, string password)
		{
			if (userName == null)
			{
                MessageBox.Show("用户名不能为空！");
				return;
            }
            if (password == null)
            {
                MessageBox.Show("密码不能为空！");
				return ;
            }
            StartLoading(o);
            string sql = "select * from login where login_name=@login_name and login_pwd=@login_pwd";
            DataTable loginInfo = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql, new MySqlParameter[] { new MySqlParameter("login_name",userName), new MySqlParameter("login_pwd", password) });
			List<Login> info = DBConvert.DataTableToList<Login>(loginInfo);
			if (info == null || info.Count == 0)
			{ 
				MessageBox.Show("用户名或密码错误，请重新输入！");
                Stop(o);
                return;
            }
            foreach (Login login in info)
			{
				if (login.Login_name == userName && login.Login_pwd == password)
				{
					Global.currentLoginUser.Login_name = login.Login_name;
					Global.currentLoginUser.Login_pwd = login.Login_pwd;
					Global.currentLoginUser.Employee_ID = login.Employee_ID;
					Global.currentLoginUser.Login_power = login.Login_power;
					StopLoading(o);
				}
			}
		}

        private void StartLoading(LoginWindow o)
        {
            loadingWindow.Owner = o;//设置弹出层在主窗体上方
            loadingWindow.Show();//打开弹出层
            o.IsEnabled = false;//设置主窗体不可用
        }
        private void Stop(LoginWindow o)
        {
            loadingWindow.Hide();//打开弹出层
            o.IsEnabled = true;//设置主窗体不可用
        }
        private void StopLoading(LoginWindow o)
        {
            Timer t1 = new Timer(3000);
            t1.Elapsed += new ElapsedEventHandler((s, e) => Stop(s, e, o));
            t1.Enabled = true;
			t1.AutoReset = false;
            t1.Start();

        }

		private void Stop(object? sender, ElapsedEventArgs e, LoginWindow o)
		{
			loadingWindow.Dispatcher.Invoke(new Action(() => { loadingWindow.Hide(); }));
			//loadingWindow.Hide();//弹出层隐藏（如果直接Close的话，只能打开弹出层一次）
			o.Dispatcher.Invoke(new Action(() => {
				o.IsEnabled = true;//主窗体可用
                new MainWindow().Show();
                o.Close();
            }));

        }
	}
}
